package com.core.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.core.system.service.ISystemPermissionService;
import com.core.system.utils.ConvertUtils;
import com.pojo.entity.SystemPermissionEntity;
import com.pojo.req.RemoveReq;
import com.pojo.req.permission.AddPermission;
import com.pojo.req.permission.PagePermissionReq;
import com.pojo.req.permission.TemplatePermissionReq;
import com.pojo.req.permission.UpdatePermission;
import com.pojo.resp.permission.PagePermissionResp;
import com.utility.constant.Constant;
import com.utility.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api/system/permission")
public class PermissionController {

    @Autowired ISystemPermissionService iSystemPermissionService;


    /**
     * 添加权限
     * @param addPermission
     * @return
     */
    @PostMapping("/add")
    public Result insertPermission(@RequestBody @Validated AddPermission addPermission){
        iSystemPermissionService.save(ConvertUtils.Convert(addPermission,SystemPermissionEntity.class));
        return Result.returnCodeSuccess();
    }

    /**
     * 分页
     * @param permission
     * @return
     */
    @GetMapping("/page")
    public Result pagePermission(PagePermissionReq permission){
        IPage<PagePermissionResp> page = iSystemPermissionService.page(permission);
        return Result.returnCodeSuccess(page);
    }




    /**
     * 编辑权限
     * @param updatePermission
     * @return
     */
    @PostMapping("/update")
    public Result updatePermission(@RequestBody @Validated UpdatePermission updatePermission){
        iSystemPermissionService.updateById(ConvertUtils.Convert(updatePermission,SystemPermissionEntity.class));
        return Result.returnCodeSuccess();
    }

    /**
     * 编辑权限
     * @param removeReq
     * @return
     */
    @PostMapping("/delete")
    public Result deletePermission(@RequestBody @Validated RemoveReq removeReq){
        SystemPermissionEntity convert = ConvertUtils.Convert(removeReq, SystemPermissionEntity.class);
        convert.setIsDelete(Constant.ONE);
        iSystemPermissionService.updateById(convert);
        return Result.returnCodeSuccess();
    }



    /**
     * 查询菜单权限
     * @param templatePermissionReq
     * @return
     */
    @PostMapping("/selectRolePermission")
    public Result selectRolePermission(@RequestBody @Validated TemplatePermissionReq templatePermissionReq){
        return Result.returnCodeSuccess(iSystemPermissionService.selectRolePermission(templatePermissionReq));
    }

}
